package org.restql.db.resource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import org.apache.commons.dbcp.BasicDataSource;
import org.restql.db.Utility;
import org.restql.db.WhereClauseGenerator;
import org.restql.db.resource.RequestResource;

public class AutomaticDatabaseResourceImpl extends ModifiableDatabaseResourceImpl {

	Hashtable<String, BasicDataSource> dataSources = new Hashtable<String, BasicDataSource>();
	
	public AutomaticDatabaseResourceImpl() {
		this.select = new ArrayList<String>();
		this.select.add("*");
		this.where = new ArrayList<String>();
	}

    @Override
    public Connection getConnection(RequestResource requestResource) throws SQLException {
        //String jdbcUrl = "jdbc:mysql://localhost:3306/" + requestResource.getDbName();
    	//String connectString = jdbcUrl + "||" + requestResource.getUser() + "||" + 
    	//	requestResource.getPassword();
    	
    	this.jdbcUrl = requestResource.getJdbcUrl();
    	String connectString = requestResource.getConnectString();
    	this.jdbcDriver = requestResource.getJdbcDriver();;
    	this.user = requestResource.getUser();
    	this.password = requestResource.getPassword();
        
        BasicDataSource dataSource = null;
        if (dataSources.containsKey(connectString)) {
        	dataSource = dataSources.get(connectString);
        } else {
        	dataSource = new BasicDataSource();
        	dataSource.setDriverClassName(jdbcDriver);
        	dataSource.setUsername(requestResource.getUser());
        	dataSource.setPassword(requestResource.getPassword());
        	dataSource.setUrl(jdbcUrl);
        	dataSources.put(connectString, dataSource);
        }
        return dataSource.getConnection();
    }

	public String getSelectString(RequestResource requestResource, String max, String skip) {
		return WhereClauseGenerator.generateSelectString(requestResource, false, select, where, getConnectString(), max, skip);
	}
	

    public String getInsertString(RequestResource requestResource, List<String> fieldList, List<String> valuesList) {
		return "INSERT INTO " + requestResource.getResourceName() +		
		" (" + Utility.commas(fieldList) +
		") values (" + Utility.commasAndQuotes(valuesList) + ")";
		
	}

	public String getUpdateString(RequestResource requestResource, List<String> fieldList) {
		return "UPDATE " + requestResource.getResourceName() +
		" SET " + Utility.commas(fieldList) +
		WhereClauseGenerator.getWhereString(requestResource, where);
	}

    public boolean reissueCredentials() {
    	return true;
    }

    public String getDeleteString(RequestResource requestResource) {
		String whereClause = WhereClauseGenerator.getWhereString(requestResource, where);
		String constructedSQL = "DELETE " + WhereClauseGenerator.getFromString(requestResource)
				+ whereClause;
		return constructedSQL;
	}

	public String getJdbcDriver() {
		return jdbcDriver;
	}

	public void setJdbcDriver(String jdbcDriver) {
		this.jdbcDriver = jdbcDriver;
	}

	public String getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}
}
